package view;


import controller.DeleteGroupController;
import controller.GroupChatController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class DeleteGroupView {
    private final DeleteGroupController controller = new DeleteGroupController();
    private final GroupChatController groupChatController = new GroupChatController();

    public void showUi() {
        JFrame jf = new JFrame("删除群聊");
        jf.setIconImage(Toolkit.getDefaultToolkit().createImage(MainView.class.getResource("qq.jpg")));
        JPanel jp = new JPanel();
        JLabel jLabel = new JLabel("请选择需删除群聊的名称");
        jLabel.setFont(new Font("黑体", Font.BOLD, 24));
        JButton bu2 = new JButton("删    除");
        JButton bu3 = new JButton("退    出");
        String[] allGroupNames = groupChatController.getAllGroupNames();
        JComboBox<String> jComboBox = new JComboBox<>(allGroupNames);
        jComboBox.setFont(new Font("宋体", Font.BOLD, 24));
        jp.setLayout(null);
        jLabel.setBounds(100, 50, 400, 50);
        jComboBox.setBounds(100, 100, 300, 50);
        bu2.setBounds(175, 200, 100, 50);
        bu3.setBounds(175, 300, 100, 50);
        jf.getRootPane().setDefaultButton(bu2);
        jp.add(jLabel);
        jp.add(jComboBox);
        jp.add(bu2);
        jp.add(bu3);
        jf.add(jp);
        jf.setVisible(true);
        jf.setBounds(600, 200, 500, 500);

        bu2.addActionListener(e -> {
            Object selectedItem = jComboBox.getSelectedItem();
            if (selectedItem == null) {
                JOptionPane.showMessageDialog(null, "未进行选择");
            } else {
                controller.deleteGroup((String) selectedItem);
                controller.deleteGroupMessage((String) selectedItem);
                JOptionPane.showMessageDialog(null, "删除成功");
                jComboBox.setModel(new DefaultComboBoxModel<>(groupChatController.getAllGroupNames()));
            }
        });
        bu3.addActionListener(e -> jf.dispose());
        jf.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                jf.dispose();
            }
        });
    }
}